﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.Pages.SCS.Model;
using Twi.COMMON.Core;

namespace Twi.Pages.SCS.BLL
{
    public class _KindlyTip
    {
        private static readonly _KindlyTip instance = new _KindlyTip();
        private _KindlyTip() { }
        public static _KindlyTip Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 获取规定一天内共需要打卡的次数
        /// </summary>
        /// <returns>打卡次数</returns>
        public int GetCheckTotalCount()
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TKindlyTip
                        where Convert.ToDateTime(p.FAfterDate) <= DateTime.Now.Date
                        group p by p.FAfterDate into g
                        orderby g.Key descending
                        select g;
                var firstItem = q.First().Count();
                return firstItem;
            }
        }

        /// <summary>
        /// 获取当前时刻点的打卡提示信息
        /// </summary>
        /// <returns></returns>
        public TKindlyTip GetNowKindlyTip()
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TKindlyTip
                        where Convert.ToDateTime(p.FAfterDate) <= DateTime.Now.Date && string.Compare(p.FBeforeTime, DateTime.Now.ToString("HH:mm:ss")) > 0
                        orderby p.FAfterDate descending, p.FBeforeTime ascending
                        select p;
                return q.FirstOrDefault();
            }
        }

        /// <summary>
        /// 获取当前时间的前一个打卡限定时间
        /// <remarks>1、如：假定每天需要上下班打卡两次（08:30~17:30），设置打卡限定时间为12:00,
        /// 含义为：12点前（含12点）能打上班卡，12点后只能打下班卡
        /// 那么当前时刻时间小于等于12:00:00,返回00:00:00,否则返回12:00:00
        /// 2、如：假定每天需要打卡四次（08:30~11:30,13:00~17:30）,设置打卡限定时间为10:30,12:30,16:00，
        /// 含义为：10:30前打上午上班的卡，10:30后12:30前可打下班卡，12:30后16:00前能打下午上班卡，16:00之后能打下午下班卡
        /// 那么当前时刻时间小于等于10:30:00 返回00:00:00 ,当大于10:30:00但小于等于12:30:00，返回10:30:00,以此类推
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public string GetBeforeNowSepartorTime()
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TKindlyTip
                        where  Convert.ToDateTime(p.FAfterDate) <= DateTime.Now.Date
                        orderby p.FAfterDate descending,p.FBeforeTime
                        select p;
                string beforeTime = "00:00:00";
                foreach (var item in q)
                {
                    if (string.Compare(item.FBeforeTime, DateTime.Now.ToString("HH:mm:ss")) > 0)
                    {
                        break;
                    }
                    else
                    {
                        beforeTime = item.FBeforeTime;
                    }
                }
                return beforeTime;
            }
        }
    }
}
